using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyCon : MonoBehaviour
{
    private Transform target;
    private float currentSpeed;
    public float maxSpeed;
    private Rigidbody2D rig;
    private Animator ani;
    public float attackRate;
    private float lastAttackTime;
    public float chaseRadius;
    public float attackRadius;


    private void Awake()
    {
        rig = GetComponent<Rigidbody2D>();
        ani = GetComponent<Animator>();
        currentSpeed = maxSpeed;
        target = GameObject.FindWithTag("Player").transform;
    }

    void FixedUpdate()
    {
        if (PlayerCon.isPause) return;
        //float h = Input.GetAxisRaw("Horizontal");
        //float v = Input.GetAxisRaw("Vertical");
        //if (h != 0 || v != 0)
        //    ani.SetInteger("State", 1);
        //else
        //    ani.SetInteger("State", 0);
        if (Vector3.Distance(target.position, transform.position) <= chaseRadius)
        {
            CanMove();
            Vector3 temp = Vector3.MoveTowards(transform.position, target.position, currentSpeed * Time.deltaTime);
            ani.SetInteger("State", 1);
            rig.MovePosition(temp);
            if (Vector3.Distance(target.position, transform.position) <= attackRadius)
            {
                StopMoving();
                ani.SetInteger("State", 0);
                if (Time.time - lastAttackTime > attackRate)
                {
                    lastAttackTime = Time.time;
                    ani.SetTrigger("Attack");
                }
            }
        }
        else
        {
            StopMoving();
            ani.SetInteger("State", 0);
        }

    }

    public void StopMoving()
    {
        currentSpeed = 0;
    }
    public void CanMove()
    {
        currentSpeed = maxSpeed;
    }




}
